<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/12/26
 * Time: 13:23
 */
namespace app\admin\controller;
use app\common\lib\db\DbCommon;
use app\common\lib\exception\ApiException;
use think\Db;
use think\Model;
use think\Request;
class Notice extends Base{
    public function index(){
        if (request()->isAjax()) {
            extract(input()); //将接收的参数赋值给变量
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $map = array();
            //用户模糊昵称
            if(isset($keyword) && $keyword!="")
            {
                $map['notice_content'] = ['like',"%" . $keyword . "%"];
            }
            //用户类别
            if(isset($notice_type) && $notice_type!="")
            {
                $map['notice_type'] = $notice_type;
            }
            try{
                $count = Db::name('notice')->where($map)->count();  //总条目数据
                $lists = Db::name('notice')->page($NowPage,$limits)->where($map)->select();  //当前页
            }catch (\Exception $e) {
                return json(['code' => 500, 'msg' => $e->getMessage()]);
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }

    public function add()
    {
        if(\request()->isPOST()){
            $data = input('post.');
            $is_array = check_array($data);
            if(!$is_array) {
                throw new ApiException('非法请求','500');
            }
            $data['notice_time'] = date('Y-m-d H:i:s');
            try{
                $ret = Model('Notice')->allowField(true)->save($data);
            }catch (\Exception $e) {
                throw new ApiException($e->getMessage());
            }
            if(!$ret) {
                throw new ApiException('数据库错误','200');
            }
            return show('200','发布成功',$data);
        }
        return $this->fetch();
    }

    public function update()
    {
        if(\request()->isPOST()){
            $data = input('post.');
            $data['notice_update_time'] = date('Y-m-d H:i:s');
            $res = Model('Notice')->save($data,['notice_id'=>$data['notice_id']]);
            if(!$res) {
                throw new ApiException('没有做任何更改','200');
            }
            return show('200','已更新',$res);
        }else {
            $notice_id = Request::instance()->param('notice_id');
            $data = Db::name('notice')->where(['notice_id' => $notice_id])->find();
            $this->assign([
                'data' => $data,
            ]);
            return $this->fetch();
        }
    }

    /**
     * Ajax抛送删除
     * @return \think\response\Json
     */
    public function del()
    {
        $user_id = Request::instance()->param('id');
        $res = Model('Notice')->where(['notice_id'=>$user_id])->delete();
        if(!$res) {
            return json(['code'=>500,'msg'=>'数据库错误']);
        }
        return json(['code'=>200,'msg'=>'已删除']);
    }

    /**
     * 查看是否已经删除，而不是删除继续点击删除
     * @param number $user_id 用户id
     * @return bool $is_del
     */
    public function is_del($user_id)
    {
        $res = Model('UUser')->get(['user_id' => $user_id,'user_status' => '4']);
        if(!$res) {
            return false;
        }
        return true;
    }

    /**
     * @param number $start_time
     * @param number $end_time
     * @param number $month_time
     * @return bool 升级结果
     */
    public function up_vip()
    {
        if(\request()->isPost()){
            $data = input('post.');
            $is_vip = $this->is_vip($data['user_id']);
            if($is_vip) {
                return show('200','当前用户已是vip，无需升级',[]);
            }
            $vip_info = array();
            $vip_info['start_time'] = time();
            $vip_info['end_time'] = $vip_info['start_time'] + $data['month_time'] * 24 * 60 * 60 * 30;
            $vip_info['is_vip'] = 2;
            $vip_info['start_time'] = date("Y-m-d H:i:s", $vip_info['start_time']);
            $vip_info['end_time'] = date("Y-m-d H:i:s", $vip_info['end_time']);
            try{
                $up_vip = Model('UUser')->save($vip_info,['user_id'=>$data['user_id']]);
            }catch (\Exception $e){
                return show('200','ok',$e->getMessage());
            }
            if(!$up_vip) {
                return show('200','数据库错误',[]);
            }
            return show('200','已升级成'.$data['month_time'].'月会员',[]);
        }else {
            $user_id = Request::instance()->param('user_id');
            $data = Db::name('u_user')->where(['user_id' => $user_id])->find();
            $this->assign(['data'=>$data]);
            return $this->fetch();
        }
    }

    /**
     * 当前用户是否是会员
     * @param number $user_id
     * @return bool|string
     */
    public function is_vip($user_id)
    {
        if(!isset($user_id) || empty($user_id) ||!is_numeric($user_id)){
            return show('200','参数不合法',[]);
        }
        $ret = Model('UUser')->get(['user_id' => $user_id,'is_vip'=>'2']);
        if(!$ret) {
            return false;
        }
        return true;
    }
}